package com.icloud.article.boot.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icloud.article.boot.mapper.ArticleVisibleCustomerMapper;
import com.icloud.article.boot.model.ArticleVisibleCustomer;
import com.icloud.article.boot.service.ArticleVisibleCustomerService;
import com.icloud.common.database.enums.ArticleVisibleEnum;
import com.icloud.common.database.enums.DeleteEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 帖子可见用户（帖子设置了仅好友可见时存在数据）service
 *
 */

@Service
public class ArticleVisibleCustomerServiceImpl extends ServiceImpl<ArticleVisibleCustomerMapper, ArticleVisibleCustomer> implements ArticleVisibleCustomerService {


    /**
     * 批量添加不可见客户
     * @param articleId         帖子id
     * @param authorId          作者id
     * @param visibleIds       不可见的客户ids
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void batchAddForbidden(Long articleId, Long authorId, List<Long> visibleIds) {
        if (CollectionUtil.isEmpty(visibleIds)){
            return;
        }

        //删除老数据
        update(new LambdaUpdateWrapper<ArticleVisibleCustomer>()
                .set(ArticleVisibleCustomer::getIsDelete, DeleteEnum.DELETED.getCode())
                .eq(ArticleVisibleCustomer::getArticleId,articleId));

        List<ArticleVisibleCustomer> list = new ArrayList<>();
        for (Long visibleId : visibleIds) {

            ArticleVisibleCustomer articleVisibleCustomer = new ArticleVisibleCustomer();
            articleVisibleCustomer.setArticleId(articleId);
            articleVisibleCustomer.setVisibleId(visibleId);
            articleVisibleCustomer.setIsVisible(ArticleVisibleEnum.FALSE.getCode());
            articleVisibleCustomer.setAuthorId(authorId);
            list.add(articleVisibleCustomer);
        }

        saveBatch(list);
    }
}
